package kr.bos;

import java.util.ArrayList;

import kr.bos.Define.Key;
import kr.bos.Framework.BaseActivity;
import kr.bos.Framework.Logger;
import kr.bos.Services.UnderlyingBackground;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends BaseActivity{
	
	PriceAdapter m_adListController = null;
	ListView m_lvActivityList = null;
	final String prefix = "kr.bos.Subclasses.";
	
	@SuppressWarnings("unchecked") @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setResource();
		initialization();
		PackageInfo manager = null;
		try {
			manager = getPackageManager().getPackageInfo(getPackageName(),
					PackageManager.GET_ACTIVITIES);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if(manager!=null) {
			ActivityInfo[] actInfo = manager.activities;
			
			for(int i = 0 ; actInfo!=null && i < actInfo.length ; i++) {
				String activityName = String.format(actInfo[i].name, i+1);
				if(!activityName.contains(prefix))
					continue;
				
				@SuppressWarnings("unused") Class _cls = null;
				try {
					_cls = Class.forName(activityName);
					String[] arr = new String[4];
					arr[0] = activityName.replace(prefix, "");
					arr[1] = "a"+i*2;
					arr[2] = String.valueOf(false);
					arr[3] = String.valueOf(false);
					m_adListController.m_Data0.add(arr);
				} catch (ClassNotFoundException e) {
					Logger.v("Finding "+activityName +" but failed..");
					continue;
				}
			}
		}
		
		Intent ittService = new Intent(getApplicationContext(),UnderlyingBackground.class);
		startService(ittService);
	}
	
	private void setResource() {
		View linListLay = getLayoutInflater().inflate(R.layout.main_listview, null);
		((LinearLayout)findViewById(R.id.mainbody)).addView(linListLay);
		
		m_adListController = new PriceAdapter(this);
		m_lvActivityList = (ListView)findViewById(R.id.mainListView1);
		m_adListController.notifyDataSetChanged();
	}

	private void initialization() {
		
		m_lvActivityList.setAdapter(m_adListController);
		m_lvActivityList.setDivider(null);
		m_lvActivityList.setOnItemClickListener(onItemClick);		
	}

	private class ViewHolder{
		TextView tvTitle;
		TextView tvContext;
		ToggleButton togA;
		ToggleButton togB;
	}
	
	private class PriceAdapter extends BaseAdapter
	{
		
		private ArrayList<String[]> m_Data0 = new ArrayList<String[]>();
		private LayoutInflater mInflater;

		public PriceAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return m_Data0.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return getViewDefault(position, convertView, parent);
		}

		View getViewDefault(int position, View convertView, ViewGroup parent){
			final ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_main, null);
				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.mainListTitle);
				holder.tvContext = (TextView) convertView.findViewById(R.id.mainListContext);
				holder.togA = (ToggleButton) convertView.findViewById(R.id.mainListToggle1);
				holder.togB = (ToggleButton) convertView.findViewById(R.id.mainListToggle2);
				convertView.setTag(holder);
				
				holder.tvContext.setVisibility(View.GONE);
			} else {
				holder = (ViewHolder) convertView.getTag(R.string.TAG_VIEW);
			}
			String[] temp = m_Data0.get(position);

			holder.tvTitle.setText(temp[0]);
			holder.tvContext.setText(temp[1]);
			holder.togA.setChecked(Boolean.parseBoolean(temp[2]));
			holder.togA.setChecked(Boolean.parseBoolean(temp[3]));
			
			convertView.setTag(R.string.TAG_VIEW,holder);
			convertView.setTag(R.string.TAG_OBJ,temp);

			return convertView;
		}
	}
	
	
	OnItemClickListener onItemClick = new OnItemClickListener() {
		@SuppressWarnings("unchecked") @Override
		public void onItemClick(AdapterView<?> arg0, View v,int arg2, long arg3) {
			Logger.v("ID "+v.getId());
			String result[] = (String [])v.getTag(R.string.TAG_OBJ);
			ViewHolder views = (ViewHolder)v.getTag(R.string.TAG_VIEW);

			Class _cls = null;
			try {
				_cls = Class.forName(prefix+result[0]);
			} catch (ClassNotFoundException e) {

			}
			String[] arr = new String[2];
			arr[0] = String.valueOf(views.togA.isChecked());
			arr[1] = String.valueOf(views.togB.isChecked());

			//가져온 클래스로 인텐트 생성
			Intent _intent = new Intent(getApplicationContext(),_cls);
			_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			_intent.putExtra(Key.BUNDLE, arr);
			startActivity(_intent);
		}
	};
	
	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Logger.v("ID "+v.getId());
		}
	};
	
}